package com.company.project.robot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.company.project.common.utils.DataResult;
import com.company.project.robot.entity.ReceiveEntity;
import com.company.project.robot.entity.SendMsgEntity;
import com.company.project.robot.entity.WechatGroupEntity;
import com.company.project.robot.service.RobotService;
import com.company.project.robot.service.WechatGroupService;
import com.company.project.robot.service.impl.DispatchServiceImpl;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author ZYF
 * @date 2021/10/28 15:08
 */
@Api(tags = "机器人开放接口")
@RestController
@Slf4j
@RequestMapping("/openApi")
public class OpenApiController {

    @Autowired
    DispatchServiceImpl dispatchService;

    @RequestMapping("/catApi")
    public String callBack(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {

        ReceiveEntity receive = JSONObject.toJavaObject(jsonObject, ReceiveEntity.class);


        log.info("-----------------开始----------------------");

//        SendMsgEntity msg = new SendMsgEntity();
//        msg.setRequestId(toInt(request.getParameter("rid")));
//        msg.setMsgType(toInt(request.getParameter("msg_type")));
//        msg.setType(toInt(request.getParameter("type")));
//        msg.setMsgId(toInt(request.getParameter("msg_id")));
//        msg.setRobotWxid(request.getParameter("robot_wxid"));
//        msg.setFromWxid(request.getParameter("from_wxid"));
//        msg.setFromName(request.getParameter("from_name"));
//        msg.setFinalFromWxid(request.getParameter("final_from_wxid"));
//        msg.setFinalFromName(request.getParameter("final_from_name"));
//        msg.setMsg(request.getParameter("msg"));
//        msg.setMsgTime(toInt(request.getParameter("time")));
//        msg.setFileUrl(request.getParameter("file_url"));
        try {
            dispatchService.sendMsg(receive);
//            dispatchService.disp(msg);
        }  catch (Exception e) {
            log.error("有错误了"+e.toString());
        }
        String result = "rtn ok";
        log.info("-----------------结束----------------------");
        return result;
    }

    @RequestMapping("/test")
    public String test(){
        dispatchService.test();
        return "aaa";
    }

    private Integer toInt(String agr){
        return agr!=null ? Integer.valueOf(agr) : 0;
    }

    @Autowired
    private WechatGroupService wechatGroupService;


    @GetMapping("/send")
    public DataResult send() throws IOException {
        WechatGroupEntity wechatGroup = wechatGroupService.getById("25036389795@chatroom");
        //由哪个机器人去发送
        com.company.project.robot.sdk.Api.send_file_msg("http://192.168.8.129:8073/send", wechatGroup.getRobotId(), wechatGroup.getId(),URLEncoder.encode("C:\\Users\\Administrator\\Desktop\\台博会v2.0.3抽奖活动优化验收单.xlsx", "UTF-8"));
        return DataResult.success();
    }

}
